package one.stand.controller;

import com.jmp.api.user.dto.CustomerAddRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.MessagePushRequest;
import com.jmp.base.dto.MessagePushType;
import com.jmp.feign.CrmFegin;
import com.jmp.feign.MessageFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.request.CustomerRequest;
import one.stand.service.BusinessCardBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("userApi/customer")
@Api(description = "客户管理")
public class CustomerController {
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CrmFegin crmFegin;
    @Autowired
    private MessageFeign messageFeign;

    @ApiOperation(value = "存为客户")
    @PostMapping("add")
    @SecurityAuthCheck(card = true)
    public ResultModel add(@RequestAttribute CustomerAddRequest request) {

        // add customer
        BusinessCard businessCardModel = businessCardBaseService.getCustomer(request.getCardId());
        CustomerRequest customerRequest = new CustomerRequest();
        customerRequest.setUserId(request.getId());
        customerRequest.setName(businessCardModel.getName());
        customerRequest.setPhone(businessCardModel.getPhone());
        customerRequest.setPosition(businessCardModel.getPosition());
        customerRequest.setLogo(businessCardModel.getLogo());
        customerRequest.setEmail(businessCardModel.getEmail());
        customerRequest.setPersonalWx(businessCardModel.getPersonalWx());

        customerRequest.setCId(Long.valueOf(request.getCId()));
        customerRequest.setMyCardId(request.getMyCardId());
        customerRequest.setId(Long.valueOf(request.getId()));
        customerRequest.setCustomerState(1);//1 独占
        customerRequest.setEntrance(2);//1 手动添加 2 存为客户
        customerRequest.setFollowType("1");//标签：1 不添加跟进记录
        ResultModel<Boolean> resultModel = crmFegin.manualAdd2(customerRequest);
        if (!resultModel.isSuccess()) {
            return resultModel;
        }

        // push
        MessagePushRequest pushRequest = MessagePushRequest.builder()
                .type(MessagePushType.CUSTOMER_ADD)
                .toId(request.getId())
                .pageParams(new Integer[]{request.getCardId()})
                .build();
        messageFeign.push(pushRequest);
        return ResultModel.success();
    }
}
